package img;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Random;
import java.util.UUID;

/**
 * @author wzby
 * @Date: 2022/8/3 20:56
 */
//验证码代码
public class Ex2 {
    public static void main(String[] args) {
        for (int i = 0; i < 50; i++) {
            try {
                check();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void check() throws Exception {
        int w = 100;
        int h = 50;
        Random rand = new Random();
        var ima = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        var g = ima.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);

        g.setColor(Color.WHITE);
        g.fillRect(0, 0, w, h);

        Font z = Font.createFont(Font.TRUETYPE_FONT, new File("E:\\uexdir\\font\\c.ttf"));
        z = z.deriveFont(Font.PLAIN, 25);

        g.setFont(z);

        Color c = new Color(255, 0, 0);
        g.setColor(c);
        //验证码
        String str = getRandStr();
        g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(180, 256)));
        for (int i = 0; i < str.length(); i++) {
            int x = i * 25 + rand.nextInt(2, 6);
            int y = rand.nextInt(25, 45);
            g.drawString(str.substring(i, i + 1), x, y);
        }
        System.out.println(str);

        String str2 = getRandStr(rand.nextInt(8, 20));
        for (int i = 0; i < str2.length(); i++) {
            g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256), rand.nextInt(100, 150)));
            int x = rand.nextInt(w);
            int y = rand.nextInt(h);
            z = z.deriveFont(1.0f * rand.nextInt(15, 20));
            g.setFont(z);
            g.drawString(str2.substring(i, i + 1), x, y);
        }
        for (int i = 0 ;i < 15;i++){
            int x1 = rand.nextInt(w);
            int x2 = rand.nextInt(w);
            int y1 = rand.nextInt(h);
            int y2 = rand.nextInt(h);
            g.setStroke(new BasicStroke(rand.nextInt(1,5)));
            g.setColor(new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256),rand.nextInt(30,50)));
            g.drawLine(x1,x2,y1,y2);
        }
        g.dispose();
        ImageIO.write(ima,"png",new File("E:\\uexdir\\yzm\\" + UUID.randomUUID().toString() + ".png"));
    }

    //干扰文字
    public static String getRandStr() {
        return getRandStr(4);
    }

    public static String getRandStr(int len) {
        String letter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder sbu = new StringBuilder();
        Random rand = new Random();
        for (int i = 0; i < len; i++) {
            int pos = rand.nextInt(0, letter.length());
            sbu.append(letter.substring(pos, pos + 1));
        }
        return sbu.toString();
    }
}
